
#import "LoginVC.h"
#import "RegistVC.h"
#import "ForgetPwdVC.h"

@interface LoginVC ()

@property (nonatomic, strong) UITextField *companyField;
@property (nonatomic, strong) UITextField *nameField;
@property (nonatomic, strong) UITextField *pwdField;
@property (nonatomic, strong) UIButton *showBtn;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *forgetBtn;
@property (nonatomic, strong) UIButton *registBtn;

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

- (void)configUI {
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, NaviHeight + 30, ScreenW - 80, 32)];
    titleLab.text = @"密码登录";
    titleLab.textColor = HexColor(0x2B2C44);
    titleLab.font = BoldFontOfSize(30);
    [self.view addSubview:titleLab];
    
    UILabel *companyTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, titleLab.nim_bottom + 60, titleLab.nim_width, 13)];
    companyTitleLab.text = @"公司编号";
    companyTitleLab.font = [UIFont systemFontOfSize:12];
    companyTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:companyTitleLab];
    
    self.companyField = [[UITextField alloc] initWithFrame:CGRectMake(40, companyTitleLab.nim_bottom + 2, companyTitleLab.nim_width, 40)];
    self.companyField.placeholder = @"请输入公司编号";
    self.companyField.font = MediumFontOfSize(16);
    self.companyField.textColor = HexColor(0x2B2C44);
    [self.view addSubview:self.companyField];
    
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.companyField.nim_bottom, self.companyField.nim_width, 1)];
    line1.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line1];
    
    UILabel *nameTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, line1.nim_bottom + 20, titleLab.nim_width, 13)];
    nameTitleLab.text = @"账号";
    nameTitleLab.font = [UIFont systemFontOfSize:12];
    nameTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:nameTitleLab];
    
    self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(40, nameTitleLab.nim_bottom + 2, nameTitleLab.nim_width, 40)];
    self.nameField.placeholder = @"请输入手机号";
    self.nameField.font = MediumFontOfSize(16);
    self.nameField.textColor = HexColor(0x2B2C44);
    self.nameField.keyboardType = UIKeyboardTypePhonePad;
    [self.view addSubview:self.nameField];
    
    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.nameField.nim_bottom, self.nameField.nim_width, 1)];
    line2.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line2];
    
    UILabel *pwdTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, line2.nim_bottom + 20, titleLab.nim_width, 13)];
    pwdTitleLab.text = @"密码";
    pwdTitleLab.font = [UIFont systemFontOfSize:12];
    pwdTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:pwdTitleLab];
    
    self.pwdField = [[UITextField alloc] initWithFrame:CGRectMake(40, pwdTitleLab.nim_bottom + 2, pwdTitleLab.nim_width, 40)];
    self.pwdField.placeholder = @"请输入登录密码";
    self.pwdField.font = MediumFontOfSize(16);
    self.pwdField.textColor = HexColor(0x2B2C44);
    self.pwdField.secureTextEntry = YES;
    [self.view addSubview:self.pwdField];
    
    self.showBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    [self.showBtn setImage:[UIImage imageNamed:@"pwd_hide"] forState:0];
    [self.showBtn addTarget:self action:@selector(showBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.pwdField.rightView = self.showBtn;
    self.pwdField.rightViewMode = UITextFieldViewModeAlways;
    
    UILabel *line3 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.pwdField.nim_bottom, self.pwdField.nim_width, 1)];
    line3.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line3];
    
    self.forgetBtn = [[UIButton alloc] initWithFrame:CGRectMake(40, line3.nim_bottom + 10, [FactoryMethod sizeWithText:@"忘记密码？" font:[UIFont systemFontOfSize:12] maxSize:CGSizeMake(0, 0)].width, 20)];
    [self.forgetBtn setTitle:@"忘记密码？" forState:0];
    [self.forgetBtn setTitleColor:HexColor(0x3D68EB) forState:0];
    self.forgetBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    self.forgetBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.forgetBtn addTarget:self action:@selector(forgetClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.forgetBtn];
    
    self.registBtn = [[UIButton alloc] initWithFrame:CGRectMake(line3.nim_right - [FactoryMethod sizeWithText:@"企业注册" font:[UIFont systemFontOfSize:12] maxSize:CGSizeMake(0, 0)].width, line3.nim_bottom + 10, [FactoryMethod sizeWithText:@"企业注册" font:[UIFont systemFontOfSize:12] maxSize:CGSizeMake(0, 0)].width, 20)];
    [self.registBtn setTitle:@"企业注册" forState:0];
    [self.registBtn setTitleColor:HexColor(0x3D68EB) forState:0];
    self.registBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    self.registBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [self.registBtn addTarget:self action:@selector(registClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registBtn];
    
    self.loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(40, self.forgetBtn.nim_bottom + 30, line3.nim_width, 44)];
    [self.loginBtn setBackgroundColor:HexColor(0x3D68EB)];
    self.loginBtn.layer.cornerRadius = 6;
    [self.loginBtn setTitle:@"登录" forState:0];
    [self.loginBtn setTitleColor:[UIColor whiteColor] forState:0];
    [self.loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginBtn];
}

- (void)forgetClick {
    ForgetPwdVC *vc = [ForgetPwdVC new];
    [self.rt_navigationController pushViewController:vc animated:YES];
}

- (void)registClick {
    RegistVC *vc = [RegistVC new];
    [self.rt_navigationController pushViewController:vc animated:YES];
}

- (void)showBtnClick {
    self.pwdField.secureTextEntry = !self.pwdField.isSecureTextEntry;
    [self.showBtn setImage:[UIImage imageNamed:self.pwdField.isSecureTextEntry ? @"pwd_hide" : @"pwd_show"] forState:0];
}

- (void)loginClick {
    if ([FactoryMethod isNull:self.companyField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入编号"];
        return;
    }
    if ([FactoryMethod isNull:self.nameField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入手机号"];
        return;
    }
    if (![FactoryMethod isPhone:self.nameField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入正确的手机号"];
        return;
    }
    if ([FactoryMethod isNull:self.pwdField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入密码"];
        return;
    }
    [MBProgressHUD leShowHUDWithMessage:@"登录中" activity:YES];
    [HttpRequest PostWithApi:LogisticsSupplierLogin params:@{@"company_no":self.companyField.text,@"phone":self.nameField.text,@"password":self.pwdField.text} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:MAIN_WINDOW message:@"登录成功" delayOperation:^{
                [User loginSuccessData:responseObject];
            }];
        }
    } fail:^(NSError * _Nonnull error) {

    }];
}

@end
